/**
 * @author 王晓伟
 * 部门树
 */
var  dutystore,dutywin,dutytempstore;
var  dutyid="",dutyname="" ,dutyidtager="",dutynametager="";
function initdutytree(){
	 Ext.BLANK_IMAGE_URL=Ext.get('basePath').getValue()+"ajaxFramework/ext-2.1/resources/images/default/s.gif";	  
	 var url = Ext.get('basePath').getValue()+'tjsinfo/tjsoa/system/dutytree.do';
	 var load = new Ext.tree.TreeLoader({
	 	dataUrl:url
	 });
	 var root = new Ext.tree.AsyncTreeNode({
	 	id:'0',
	 	text:'天津利达粮油有限公司'
	 })
	 var tree = new Ext.tree.TreePanel({
	 		x:0,
	 		y:0,
        	autoScroll: true,
        	animate: true,
        	containerScroll: true,
        	border: true,
	        loader:load,
	        height:420,
	        width:230,
	        anchor:'0% 100%'
	    });
	tree.setRootNode(root); 
	tree.on('dblclick',function(node){
			
			if(node.id!='0'&&dutystore.find('dutyId',node.id) ==-1)
			{
				var orderNo = 1;
				if(dutystore.getCount()>0)
				{
					orderNo	= dutystore.getAt(dutystore.getCount() - 1).get('orderNo')+1;
				}
				dutystore.add(new Ext.data.Record({
				orderNo:orderNo,
				dutyName:node.text,
				id:0,
				dutyId:node.id
			}));	
		}		
	});
	/**
	 * 生成表格
	 */
	 var dutystoreurl = Ext.get('basePath').getValue()+'tjsinfo/tjsoa/system/selectUserDuty.do?userid='+Ext.get('userid').getValue();
	 dutystore = new Ext.data.JsonStore({
		url:dutystoreurl,
		fields:['orderNo','dutyName','id','dutyId','operate']
	});
	dutytempstore = new Ext.data.JsonStore({
		url:dutystoreurl,
		fields:['orderNo','dutyName','id','dutyId','operate']
	});
	dutytempstore.load();
	dutystore.load();
	var cm = new Ext.grid.ColumnModel([
		{header: "序号", width: 25, dataIndex: 'orderNo',menuDisabled : true},
		{header: "职位名称", width: 75, dataIndex: 'dutyName',menuDisabled : true},
		{header: "操作", width: 75, dataIndex: 'operate',menuDisabled : true,renderer:function(s,m,rec){
		return "<a  href="+"javascript:dutyremove('"+rec.data.dutyId+"')>移除</a>";
		}},
		{header: "id", width: 75, dataIndex: 'id',menuDisabled : true,hidden:true},
		{header: "职位ID", width: 75, dataIndex: 'dutyId',menuDisabled : true,hidden:true}
	]);
	var grid = new Ext.grid.GridPanel({
		x:tree.width,
	 	y:0,
		cm:cm,
		width:270,
		height:420,
		store:dutystore,
		anchor:'100% 100%',
		border: true,
	   	viewConfig:{
			forceFit:true	
		}
	});
	dutywin = new Ext.Window({
		layout:'absolute',
		title:'职位选择',
		width:520,
		height:500,
		modal:true,
		closeAction:'hide',
		items:[tree,grid],
		buttonAlign:'right',
		buttons:[{
			text:'保存',
			handler:function(){
				dutyid='';
				dutyname='';
				dutystore.each(function(record){
				dutyid+=record.get('dutyId')+",";
				dutyname+=record.get('dutyName')+",";
				
				});
				$("#"+dutyidtager).attr('value',dutyid);
				$("#"+dutynametager).attr('value',dutyname);
				dutytempstore.removeAll();
				dutystore.each(function(record){
					dutytempstore.addSorted(record);	
				});
				dutywin.hide();
			}
		},{
			text:'取消',
			handler:function(){
				dutystore.removeAll();
				dutytempstore.each(function(record){
					dutystore.addSorted(record);	
				});
				dutywin.hide();		
			}
		}]
		
	})
	//win.show(Ext.getBody());
}
/**
 * 移除表格记录
 */
function dutyremove(dutyId){
	dutystore.remove(dutystore.getAt(dutystore.find('dutyId',dutyId)));
	
}
function dutywinshow(id,name){
	dutyidtager = id;
	dutynametager = name;
	dutywin.show(Ext.getBody());
	dutywin.findByType('treepanel')[0].root.expand();  
}
